package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.Materia;
import com.ylxl.entity.MateriaProgram;
import com.ylxl.entity.Program;
import com.ylxl.entity.reponse.MateriaDetailVO;
import com.ylxl.entity.request.MateriaCmd;
import com.ylxl.entity.request.MateriaRemove;
import com.ylxl.entity.request.QueryMateria;
import com.ylxl.mapper.MateriaMapper;
import com.ylxl.mapper.MateriaProgramMapper;
import com.ylxl.mapper.ProgramMapper;
import com.ylxl.service.IMateriaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 */
@Service
public class MateriaServiceImpl extends ServiceImpl<MateriaMapper, Materia>  implements IMateriaService {
    private static final Logger log = LoggerFactory.getLogger(MateriaServiceImpl.class);

    @Autowired
    private MateriaMapper materiaMapper;

    @Autowired
    private MateriaProgramMapper materiaProgramMapper;

    @Autowired
    private ProgramMapper programMapper;


    @Override
    public List<Materia> selectMateriaList(QueryMateria queryMateria){
        LambdaQueryWrapper<Materia> programLambdaQueryWrapper = new LambdaQueryWrapper<>();
        programLambdaQueryWrapper.like(StringUtils.isNotEmpty(queryMateria.getMateriaName()),Materia::getMateriaName,queryMateria.getMateriaName())
                .ge(queryMateria.getCreateTime() !=null,Materia::getCreateTime,queryMateria.getCreateTime() )
                .le(queryMateria.getEndTime()!=null,Materia::getCreateTime,queryMateria.getEndTime());
        List<Materia> materias = materiaMapper.selectList(programLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(materias)){
            //给节目数量赋值
            materias.forEach(t->{
                LambdaUpdateWrapper<MateriaProgram> materiaProgramLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                materiaProgramLambdaUpdateWrapper.eq(MateriaProgram::getMateriaId,t.getId());
                Integer programCount = materiaProgramMapper.selectCount(materiaProgramLambdaUpdateWrapper);
                t.setProgramCount(programCount);
            });
        }
        return materias;
    }

    @Override
    public int insertMateriar(MateriaCmd cmd) {
        Materia materia = new Materia();
        BeanUtil.copyProperties(cmd, materia);//属性拷贝
        return materiaMapper.insert(materia);
    }

    @Override
    public MateriaDetailVO selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("素材id不能为空");
        }
        Materia materia = materiaMapper.selectById(id);
        MateriaDetailVO materiaDetailVO = new MateriaDetailVO();
        BeanUtil.copyProperties(materia, materiaDetailVO);//属性拷贝
        //查询素材和节目的中间关联表
        LambdaQueryWrapper<MateriaProgram> materiaProgramLambdaQueryWrapper = new LambdaQueryWrapper<>();
        materiaProgramLambdaQueryWrapper.eq(MateriaProgram::getMateriaId,id);
        List<MateriaProgram> materiaPrograms = materiaProgramMapper.selectList(materiaProgramLambdaQueryWrapper);
        //查询节目表
        List<Integer> proId = materiaPrograms.stream().map(MateriaProgram::getProgramId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(proId)){
            LambdaQueryWrapper<Program> programLambdaQueryWrapper = new LambdaQueryWrapper<>();
            programLambdaQueryWrapper.in(Program::getId,proId);
            List<Program> programs = programMapper.selectList(programLambdaQueryWrapper);
            if(CollectionUtil.isNotEmpty(programs)){
                materiaDetailVO.setStatus(1);
            }
            materiaDetailVO.setProgramList(programs);
        }
        return materiaDetailVO;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return materiaMapper.deleteBatchIds(ids);
    }

    @Override
    public int remove(MateriaRemove remove) {
        if(StringUtils.isEmpty(remove.getId()) || StringUtils.isEmpty(remove.getProgramId())){
            throw new BaseException("素材id和节目id不能为空!!!");
        }
        LambdaUpdateWrapper<MateriaProgram> materiaProgramLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        materiaProgramLambdaUpdateWrapper.eq(MateriaProgram::getMateriaId,remove.getId())
                .eq(MateriaProgram::getProgramId,remove.getProgramId());
        return materiaProgramMapper.delete(materiaProgramLambdaUpdateWrapper);
    }

}
